<template>
  <a-spin :spinning="confirmLoading">
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <!-- 操作按钮区域 -->
      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :loading="loading"
          class="j-table-force-nowrap"
          :pagination="false"
        >
          <span slot="bizStatus" slot-scope="text, record">
            <a-switch v-model="record.bizStatus" @change="handleStatus(record)" />
          </span>
          <span slot="showDetail" slot-scope="text, record">
            <a-popover placement="rightTop">
              <template #content>
                <p>{{record.tplPreview}}</p>
              </template>
              <template #title>
                <span>消息预览</span>
              </template>
              <a>预览</a>
            </a-popover>
          </span>
        </a-table>
      </div>
      <!-- table区域-end -->

      <!-- 表单区域 -->
    </a-card>
  </a-spin>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { postAction, getAction } from '@/api/manage'
import moment from 'moment'
import { filterObj } from '@/utils/util'

export default {
  name: 'NoteSendSetting',
  components: {},
  data() {
    return {
      description: '短信发送设置页面',
      disableMixinCreated: true,
      confirmLoading: false,
      model: {},
      queryParam: {},
      dataSource: [],
      loading: false,
      moment,
      // 表头
      columns: [
        {
          title: '消息类别',
          align: 'center',
          dataIndex: 'ruleType',
          customRender: (text, record) => {
            if (text === 54) {
              return '生日有礼'
            }
            if (text === 55) {
              return '定向赠券'
            }
          }
        },
        {
          title: '消息规则',
          align: 'center',
          dataIndex: 'ruleType',
          customRender: (text, record) => {
            if (text === 54) {
              return '场景营销生日有礼，推送短信'
            }
            if (text === 55) {
              return '场景营销定向赠券，推送短信'
            }
          }
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' }
        },
        {
          title: '操作',
          align: 'center',
          dataIndex: 'showDetail',
          scopedSlots: { customRender: 'showDetail' }
        }
      ],
      url: {
        list: '/message/setting/list',
        changeStatus: '/message/setting/changeStatus'
      }
    }
  },
  computed: {},
  created() {
    this.$set(this.queryParam, 'tplType', 'SMS')
    this.searchQuery1()
  },
  methods: {
    searchQuery1() {
      this.loadData(1)
    },
    getQueryParams() {
      //获取查询条件
      let sqp = {}
      var param = Object.assign(sqp, this.queryParam)
      return filterObj(param)
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }

      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleStatus(record) {
      let param = {
        id: record.id,
        bizStatus: record.bizStatus ? 1 : 0
      }
      postAction(this.url.changeStatus, param).then(res => {
        if (res.result) {
          this.$message.info('操作成功')
          this.loadData()
        } else {
          this.$message.warn(res.message)
          this.loadData()  
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>